#!/usr/bin/env node

const http = require('http'),
      fs = require('fs');
try{
  let html = fs.readFileSync('./index.html').toString('utf8');
  http.createServer((req, res) => {
    if(req.url !== '/' && req.url !== '/getlist'){
      res.statusCode = 404;
      res.setHeader('content-type','text/html');
      res.end(`<h1>Not Found!</h1>`);
      return;
    }
    if(req.url === '/'){
      res.end(html);
    }else if(req.url === '/getlist') {
      let movies = fs.readFileSync('./movieData.json');
      res.end(movies);  
    }
  }).listen(8080);
}catch(e){
  console.error(e.message);
}
